//
//  ZanDao.m
//  GolfFriend
//
//  Created by Vescky on 14-3-1.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "ZanDao.h"

#define TB_ZAN_LIST @"tb_zan_list"

@implementation ZanDao

static ZanDao *defualtZanDao;

+ (id)shareZanDao {
    if (!defualtZanDao) {
        defualtZanDao = [[ZanDao alloc] init];
        DB *dbase = [[DB alloc] init];
        FMDatabase *database = [dbase getDatabase];
        [database executeUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@(id integer primary key,type text,recordId text,userId text)",TB_ZAN_LIST]];
        [database close];
    }
    return defualtZanDao;
}

- (bool)saveZanInfo:(NSDictionary*)dict {
    if (!dict) {
        return NO;
    }
    
    if ([self checkIfZaned:dict]) {
        return NO;
    }
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] initWithDictionary:dict];
    
    if (!params || [params count] < 1) {
        return NO;
    }
    
    [self insert:params inTable:TB_ZAN_LIST];
    
    return YES;
}

- (bool)checkIfZaned:(NSDictionary*)dict {
    if (!dict || [dict count] < 1) {
        return NO;
    }
    NSString *qConfition = [NSString stringWithFormat:@"select * from %@ where recordId = '%@' and type = '%@' and userId = '%@'",TB_ZAN_LIST,[dict objectForKey:@"recordId"],[dict objectForKey:@"type"],[dict objectForKey:@"userId"]];
    NSMutableArray *arr = [self queryByConfition:qConfition];
    if (arr && [arr count] > 0) {
        return YES;
    }
    return NO;
}

@end
